var singleReview = {};
singleReview.methods = {};

$(function(){

    // 图片上传显示隐藏
    singleReview.methods.reviewSingleBodyBot();

    //星星
    singleReview.methods.singleReviewStars();

    // 图片上传
    singleReview.methods.imgUpload();

    // 删除上传图片
    // singleReview.methods.imgUploadDelete();

    // 图片切换
    singleReview.methods.PictureSwitching();

    // 发表
    singleReview.methods.published();
})


//星星
singleReview.methods.singleReviewStars = function(){

    $(".shop-single-review-score-star i").each(function(){
        var starNum =  parseInt($(this).text());
        if(starNum < 1 || starNum > 10){
            $(this).text("");
            $(this).css("width",0);
            return;
        }else{
            var newStarNum = ((starNum*14.8)/74*100)/2 + "%";
            $(this).css("width",newStarNum);
            // $(this).parents(".shop-reviewAdd-stars-box").find("span").text($(this).text()+"分");
            $(this).text("");
        }
    })
}

// 图片上传显示隐藏
singleReview.methods.reviewSingleBodyBot = function(){
    $(".shop-single-review-btn").live("click", function () {
        if($(this).parents("li").find(".shop-single-reviewSingle-body-bot").css("display") == "block"){
            $(this).parents("li").find(".shop-single-reviewSingle-body-bot").hide();
            $(this).parents("li").next("li").find(".shop-single-reviewSingle-body-top").removeClass("shop-no-topBorder");
        }else{
            $(this).parents("li").find(".shop-single-reviewSingle-body-bot").show();
            $(this).parents("li").next("li").find(".shop-single-reviewSingle-body-top").addClass("shop-no-topBorder");
        }
    })
}



// 图片上传
singleReview.methods.imgUpload = function(){
    $(".shop-user-reviewSingle-con-box").on("change", ".shop-singleReview-input-file input", function () {
        var _that = $(this);
        var s = _that.val();
        var start = s.indexOf(".") + 1;
        var name = s.substring(start, s.length).toLowerCase();
        if (name != "jpg" && name != "png" && name != "gif" && name != "jpeg") {
            Controller.Alert("图片格式必须为jpg、png、gif中的一种",true,3000);
            return;
        }
        _that.prop('type','file');
        var file = _that[0].files[0];
        
        var reader = new FileReader();
        
        var imgFile;
        
        reader.onload=function(e) {
            imgFile = e.target.result;
            _that.parents(".shop-singleReview-upload").find(".shop-singleReview-uploadImg").append("<a><img src=" + imgFile + " ><span></span></a>");
            Controller.Alert("图片上传成功", true, 1000);
            
            singleReview.methods.imgUploadNum(_that);
            singleReview.methods.imgUploadDelete(_that);
        };
        _that.prop('type','text'); 
        _that.prop('type','file');
        
        if(file == null){
            return false;
        }else{
            reader.readAsDataURL(file);
        }  
    })
}

// 图片上传数量限制
singleReview.methods.imgUploadNum = function(obj){
    var num = obj.parents(".shop-single-singleReview-imgBox").find(".shop-singleReview-uploadImg").find("a").length;
    obj.parents(".shop-singleReview-input-file").find(".shop-input-file-num span").text(num);
    if(num <= 4){
        obj.parent(".shop-singleReview-input-file").show();
    }else{
        obj.parent(".shop-singleReview-input-file").hide();
        return;
    }
}

// 删除上传图片
singleReview.methods.imgUploadDelete = function(obj){
    $(".shop-singleReview-upload").find(".shop-singleReview-uploadImg a span").on("click",function(){
        $(this).parents("a").remove();
        obj.parents(".shop-single-singleReview-imgBox").find(".shop-singleReview-uploadImgBig").hide();
        singleReview.methods.imgUploadNum(obj);
    })
}

// 图片切换
singleReview.methods.PictureSwitching = function () {
    $(".shop-singleReview-upload").on("click",".shop-singleReview-uploadImg a",function () {
        if ($(this).hasClass("shop-img-selected")) {
            $(this).removeClass("shop-img-selected");
        } else {
            $(this).parents(".shop-single-reviewSingle-body .shop-singleReview-uploadImg").find("a").each(function () {
                $(this).removeClass("shop-img-selected");
            });
            $(this).addClass("shop-img-selected");
        }
        singleReview.methods.UpdateBigimg($(this));
    });
}

// 放大图
singleReview.methods.UpdateBigimg = function (obj) {
    var imgSelected = obj.parents(".shop-single-reviewSingle-body .shop-singleReview-uploadImg").find(".shop-img-selected");
    // var bigImg = obj.parents(".shop-single-reviewSingle-body").find(".shop-singleReview-uploadImgBig");
    var bigImg = obj.parents(".shop-single-singleReview-imgBox").find(".shop-singleReview-uploadImgBig");
    if(imgSelected.length == 1){
        bigImg.css("display", "block");
        bigImg.find("img").attr("src", imgSelected.find("img").attr("src"));
    }else{
        bigImg.css("display", "none");
    }
}


// 发表
singleReview.methods.published = function(){
    $(".shop-singleReview-published-btn").live("click",function(){
        var that = $(this);
        if(that.parents(".shop-single-singleReview-imgBox").find(".shop-singleReview-uploadImg a").length > 0){
            Message.Show("提示", "提交评价", true, true, 5, "sureCancle",function(){
                that.parents(".shop-single-singleReview-imgBox").find(".shop-singleReview-uploadImg").html("");
                that.parents(".shop-single-singleReview-imgBox").find(".shop-singleReview-input-file").show();
                that.parents(".shop-single-singleReview-imgBox").find(".shop-singleReview-uploadImgBig").hide();
                that.parents(".shop-single-singleReview-imgBox").find(".shop-input-file-num span").text("0");
                return true;

            },singleReview.methods.publishedCancel);
        }else{
            Controller.Alert("请上传图片", true, 2000);
        }
        
    })
}
// (取消按钮)
singleReview.methods.publishedCancel = function () {
    return true;
}


